<?php

/**
 * @package lib.famteam.phporm
 *
 */
/**
 * @package lib.famteam.phporm
 * @author polikeiji
 *
 */
class QueryWhere {
	private $_whereQuery;
	private $_stringLikeAndArray;
	private $_stringLikeOrArray;
	private $_stringEqualsAndArray;
	private $_stringEqualsOrArray;
	private $_stringNotEqualsAndArray;
	private $_stringNotEqualsOrArray;
	private $_andWhereObjectArray;
	private $_orWhereObjectArray;
	
    public function QueryWhere() {
    		$this->_whereQuery = "";
    		$this->_stringEqualsAndArray = array();
    		$this->_stringEqualsOrArray = array();
    		$this->_stringLikeAndArray = array();
    		$this->_stringLikeOrArray = array();
    		$this->_stringNotEqualsAndArray = array();
    		$this->_stringNotEqualsOrArray = array();
    		$this->_andWhereObjectArray = array();
    		$this->_orWhereObjectArray = array();
    }
    public function isNotEmptyQuery() {
    		return $this->_whereQuery != "" ||
    			count($this->_stringLikeAndArray) > 0 ||
    			count($this->_stringLikeOrArray) > 0 ||
    			count($this->_stringEqualsAndArray) > 0 ||
    			count($this->_stringEqualsOrArray) > 0 ||
    			count($this->_stringNotEqualsAndArray) > 0 ||
    			count($this->_stringNotEqualsOrArray) > 0 ||
    			count($this->_andWhereObjectArray) > 0 ||
    			count($this->_orWhereObjectArray) > 0;
    }
	public function addAnd($whereObject) {
		$this->_andWhereObjectArray[] = $whereObject;
	}
	public function addOr($whereObject) {
		$this->_orWhereObjectArray[] = $whereObject;
	}
	public function _addAnd($whereString) {
		if ($this->_whereQuery != "") {
			$this->_whereQuery .= " and ";
		}
		$this->_whereQuery .= $whereString;
	}
	public function _addOr($whereString) {
		if ($this->_whereQuery != "") {
			$this->_whereQuery .= " or ";
		}
		$this->_whereQuery .= $whereString;
	}
	public function getWhereQuery($connection) {
		foreach ($this->_stringEqualsAndArray as $columnName => $value) {
			$this->_addStringEqualsAnd($columnName, $value, $connection);
		}
		foreach ($this->_stringEqualsOrArray as $columnName => $value) {
			$this->_addStringEqualsOr($columnName, $value, $connection);
		}
		foreach ($this->_stringNotEqualsAndArray as $columnName => $value) {
			$this->_addStringNotEqualsAnd($columnName, $value, $connection);
		}
		foreach ($this->_stringNotEqualsOrArray as $columnName => $value) {
			$this->_addStringNotEqualsOr($columnName, $value, $connection);
		}
		foreach ($this->_stringLikeAndArray as $columnName => $value) {
			$this->_addStringLikeAnd($columnName, $value, $connection);
		}
		foreach ($this->_stringLikeOrArray as $columnName => $value) {
			$this->_addStringLikeOr($columnName, $value, $connection);
		}
		foreach ($this->_andWhereObjectArray as $whereObject) {
			$this->_addAnd($whereObject->getWhereQuery($connection));
		}
		foreach ($this->_orWhereObjectArray as $whereObject) {
			$this->_addOr($whereObject->getWhereQuery($connection));
		}
		return '(' . $this->_whereQuery . ')';
	}
	public function addEqualsNullAnd($columnName) {
		$this->_addAnd($columnName . " is null");
	}
	public function addNotEqualsNullAnd($columnName) {
		$this->_addAnd($columnName . " is not null");
	}
	public function addEqualsNullOr($columnName) {
		$this->_addOr($columnName . " is null");
	}
	public function addNotEqualsNullOr($columnName) {
		$this->_addOr($columnName . " is not null");
	}
	public function _addStringEqualsAnd($columnName, $value, $connection) {
		$this->_addAnd($columnName . " = " . Query::getQueryStringValue($value, $connection));
	}
	public function addStringEqualsAnd($columnName, $value) {
		$this->_stringEqualsAndArray[$columnName] = $value;
	}
	public function _addStringEqualsOr($columnName, $value, $connection) {
		$this->_addOr($columnName . " = " . Query::getQueryStringValue($value, $connection));
	}
	public function addStringEqualsOr($columnName, $value) {
		$this->_stringEqualsOrArray[$columnName] = $value;
	}
	public function _addStringNotEqualsAnd($columnName, $value, $connection) {
		$this->_addAnd($columnName . " != " . Query::getQueryStringValue($value, $connection));
	}
	public function addStringNotEqualsAnd($columnName, $value) {
		$this->_stringNotEqualsAndArray[$columnName] = $value;
	}
	public function _addStringNotEqualsOr($columnName, $value, $connection) {
		$this->_addOr($columnName . " != " . Query::getQueryStringValue($value, $connection));
	}
	public function addStringNotEqualsOr($columnName, $value) {
		$this->_stringNotEqualsOrArray[$columnName] = $value;
	}
	public function _addStringLikeAnd($columnName, $value, $connection) {
		$this->_addAnd($columnName . " like " . Query::getQueryStringValue($value, $connection));	
	}
	public function addStringLikeAnd($columnName, $value) {
		$this->_stringLikeAndArray[$columnName] = $value;
	}
	public function _addStringLikeOr($columnName, $value, $connection) {
		$this->_addOr($columnName . " like " . Query::getQueryStringValue($value, $connection));
	}
	public function addStringLikeOr($columnName, $value) {
		$this->_stringLikeOrArray[$columnName] = $value;
	}
	public function addNumericEqualsAnd($columnName, $value) {
		$this->_addAnd($columnName . " = " . $value);	
	}
	public function addNumericEqualsOr($columnName, $value) {
		$this->_addOr($columnName . " = " . $value);
	}
	public function addNumericNotEqualsAnd($columnName, $value) {
		$this->_addAnd($columnName . " != " . $value);	
	}
	public function addNumericNotEqualsOr($columnName, $value) {
		$this->_addOr($columnName . " != " . $value);
	}
	public function addNumericGreaterAnd($columnName, $value) {
		$this->_addAnd($columnName . " > " . $value);	
	}
	public function addNumericGreaterOr($columnName, $value) {
		$this->_addOr($columnName . " > " . $value);
	}
	public function addNumericLessAnd($columnName, $value) {
		$this->_addAnd($columnName . " < " . $value);	
	}
	public function addNumericLessOr($columnName, $value) {
		$this->_addOr($columnName . " < " . $value);
	}
	public function addNumericGreaterEqualsAnd($columnName, $value) {
		$this->_addAnd($columnName . " >= " . $value);	
	}
	public function addNumericGreaterEqualsOr($columnName, $value) {
		$this->_addOr($columnName . " >= " . $value);
	}
	public function addNumericLessEqualsAnd($columnName, $value) {
		$this->_addAnd($columnName . " <= " . $value);	
	}
	public function addNumericLessEqualsOr($columnName, $value) {
		$this->_addOr($columnName . " <= " . $value);
	}

	public function addTimestampEqualsAnd($columnName, $value) {
		$this->_addAnd($columnName . " = " . Query::getDateString($value));	
	}
	public function addTimestampEqualsOr($columnName, $value) {
		$this->_addOr($columnName . " = " . Query::getDateString($value));
	}
	public function addTimestampGreaterAnd($columnName, $value) {
		$this->_addAnd($columnName . " > " . Query::getDateString($value));	
	}
	public function addTimestampGreaterOr($columnName, $value) {
		$this->_addOr($columnName . " > " . Query::getDateString($value));
	}
	public function addTimestampLessAnd($columnName, $value) {
		$this->_addAnd($columnName . " < " . Query::getDateString($value));	
	}
	public function addTimestampLessOr($columnName, $value) {
		$this->_addOr($columnName . " < " . Query::getDateString($value));
	}
	public function addTimestampGreaterEqualsAnd($columnName, $value) {
		$this->_addAnd($columnName . " >= " . Query::getDateString($value));	
	}
	public function addTimestampGreaterEqualsOr($columnName, $value) {
		$this->_addOr($columnName . " >= " . Query::getDateString($value));
	}
	public function addTimestampLessEqualsAnd($columnName, $value) {
		$this->_addAnd($columnName . " <= " . Query::getDateString($value));	
	}
	public function addTimestampLessEqualsOr($columnName, $value) {
		$this->_addOr($columnName . " <= " . Query::getDateString($value));
	}

}
?>
